package app.server.project.main.service.db.entity.user;

import app.server.project.core.syndb.SynDBMgr;
import app.server.project.core.util.MathUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

@Data
@Entity(name = "race_click")
@TableName("race_click")
@Comment(value = "赛道点击统计表")
public class RaceClickEntity {

    @Id
    @Column(name = "id")
    @Comment("用户主键")
    @TableId("id")
    private long id;


    @TableField("click_num")
    @Column(name = "click_num")
    @Comment("点击次数")
    private long clickNum;

    @JsonIgnore
    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public static RaceClickEntity of(long id) {
        RaceClickEntity userInfoEntity = new RaceClickEntity();
        userInfoEntity.setId(id);
        userInfoEntity.setSynDb(true);
        return userInfoEntity;
    }

    public void setClickNum(long clickNum) {
        this.clickNum = clickNum;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(RaceClickEntity::getClickNum, this);
    }

    public void addClickNum(long clickNum) {
        var ret = MathUtil.canAddNum(this.clickNum, clickNum);
        this.clickNum += ret;
        this.setClickNum(this.clickNum);
    }
}
